package application;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import base.Bulletin;
import base.Loadperson;
import base.Manager;
import base.Person;

import java.net.URL;
import java.util.ResourceBundle;

import db.DB_tools;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.Label;

import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn;

public class StaffinfoForManagerController implements Initializable {
	@FXML
	private TableView<Loadperson> namelist;
	@FXML
	private TableColumn<Loadperson, String> id;
	@FXML
	private TableColumn<Loadperson, String> name;
	@FXML
	private Label staffName;
	@FXML
	private Label staffPosition;
	@FXML
	private Label gender;
	@FXML
	private Label staffLevel;
	@FXML
	private Label staffState;
	@FXML
	private Label staffid;
	@FXML
	private Button delete;
	@FXML
	private Button bstaffinfo;
	@FXML
	private Button bcheckattendance;
	@FXML
	private Button battendance_st;
	@FXML
	private Button bdaily_ass;
	@FXML
	private Button bbulletin;
	@FXML
	private Button blogout;
	
	private Main application;
	Person p=new Manager();
	
	
	public void setApp(Main application)
	{
		 this.application = application;
	     ObservableList<TableColumn<Loadperson,?>> observableList = namelist.getColumns();
	     observableList.get(0).setCellValueFactory(new PropertyValueFactory("id"));
         observableList.get(1).setCellValueFactory(new PropertyValueFactory("name"));	        
         namelist.setItems(db.DB_tools.Namelist());
	}
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		//namelist.setItems(mainapp.getPersonData())
       // Listen for selection changes and show the person details when changed.
       namelist.getSelectionModel().selectedItemProperty().addListener(
               (observable, oldValue, newValue) -> showPersonDetails(newValue));
	}
	
  
    
    private void showPersonDetails(Loadperson person) {
        if (person != null) {
            // Fill the labels with info from the person object.
            staffName.setText(person.getName());
            staffPosition.setText(person.getPosition());
            gender.setText(person.getSex());
            staffLevel.setText(person.getLevel());
            staffState.setText(person.getState());
            staffid.setText(person.getId());

            // TODO: We need a way to convert the birthday into a String! 
         
        } else {
            // Person is null, remove all the text.
        	staffid.setText("");
            staffName.setText("");
            staffPosition.setText("");
            gender.setText("");
            staffLevel.setText("");
            staffState.setText("");
        }
    }
    
 // Event Listener on Button.onAction
 	@FXML
 	public void handleDeletePerson(ActionEvent event) {
 		// TODO Autogenerated
 		int selectedIndex = namelist.getSelectionModel().getSelectedIndex();
        if (selectedIndex >= 0) {
            try{
            DB_tools.deleteStaff(namelist.getSelectionModel().getSelectedItem().getId());
            System.out.println(namelist.getSelectionModel().getSelectedItem().getId());
            namelist.getItems().remove(selectedIndex);
            }catch(Exception e)
            {
            	e.printStackTrace();
            }
            staffid.setText("");
            staffName.setText("");
            staffPosition.setText("");
            gender.setText("");
            staffLevel.setText("");
            staffState.setText("");
        } 
        /*else {
            // Nothing selected.
            Dialogs.create()
                .title("No Selection")
                .masthead("No Person Selected")
                .message("Please select a person in the table.")
                .showWarning();
        }*/
 	}
 	
	
	@FXML
	public void bbulletin(ActionEvent event){
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotoaddBulletin(m);
		}
		else{
			application.gotoBulletin(p);
		}
		
	}
	
	@FXML
	public void gotoAssignment(ActionEvent event)
	{
		if("Manager".equals(p.getLevel()))
		{
			Manager m=(Manager)p;
			application.gotoaddAssignment(m);
		}
		else{
			application.gotoAssignment(p);
		}
	}
	
	@FXML
	public void bcheckattendance(ActionEvent event)
	{
		application.gotoCheckAttendance(p);	
	}
	

	@FXML
	public void bstaffinfo(ActionEvent event)
	{
		if("Manager".equals(p.getLevel()))
		{
			Manager m=(Manager)p;
			System.out.println("I'm Manager");
			application.gotoNamelist(m);
		}
		else
		{
			application.gotoStaffinfo(p);
		}
	}
	
	@FXML
	public void bdaily_ass(ActionEvent event)
	{
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotoaddAssignment(m);
		}
		else{
			application.gotoAssignment(p);
		}
	}
	
	@FXML
	public void battendance_st(ActionEvent event){
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotonamelist(m);
		}
		else{
			String s = String.valueOf(p.getID());
			application.gotoworkdayinfo(p, s);
		}
	}
 	
 
    
}



